package com.mf.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @Author: lmf
 * @Create: 2024/7/18 13:37
 * Description:服务之间调用配置
 */

@Configuration
public class FeignConfig implements RequestInterceptor {
    /**
     * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
     */
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes == null){
            return;
        }
        //  获取请求体
        HttpServletRequest request = attributes.getRequest();
        //  获取token
        String token = request.getHeader("Token");
        //  注入feign的请求头
        template.header("Token", token);
    }
}